# coding=UTF-8
from db.mysql_conn import MysqlUtils, config, sqlStr
mysql = MysqlUtils(config)
import xlwt, xlrd
from xlutils.copy import copy
goodsNum = 0
book_name_xls = './output/book4.xls'
sheet_name_xls = 'book'
value_title = [["isbn1", "isbn2", "isbn3", "review", "rating", "paper/hard", "price", "name", "id"], ]

#设置表格样式
def set_style(name, height, width):
    style = xlwt.XFStyle()
    font = xlwt.Font()
    font.name = name
    font.bold = False
    font.color_index = 4
    font.height = height
    font.width = width
    style.font = font
    return style


def write_excel_xls(path, sheet_name, value):
    index = len(value)  # 获取需要写入数据的行数
    workbook = xlwt.Workbook()  # 新建一个工作簿
    sheet = workbook.add_sheet(sheet_name)  # 在工作簿中新建一个表格
    for i in range(0, index):
        for j in range(0, len(value[i])):
            sheet.write(i, j, value[i][j])  # 像表格中写入数据（对应的行和列）
    workbook.save(path)  # 保存工作簿
    print("xls格式表格写入数据成功！")
    getGoods()

def write_excel_xls_append(path, value):
    index = len(value)  # 获取需要写入数据的行数
    print(index)
    workbook = xlrd.open_workbook(path)  # 打开工作簿
    sheets = workbook.sheet_names()  # 获取工作簿中的所有表格
    worksheet = workbook.sheet_by_name(sheets[0])  # 获取工作簿中所有表格中的的第一个表格
    rows_old = worksheet.nrows  # 获取表格中已存在的数据的行数
    new_workbook = copy(workbook)  # 将xlrd对象拷贝转化为xlwt对象
    new_worksheet = new_workbook.get_sheet(0)  # 获取转化后工作簿中的第一个表格
    for i in range(0, index):
        for j in range(0, len(value[i])):
            new_worksheet.write(i + rows_old, j, value[i][j])  # 追加写入数据，注意是从i+rows_old行开始写入
    new_workbook.save(path)  # 保存工作簿
    print("xls格式表格【追加】写入数据成功！")

# def read_excel_xls(path):
#     workbook = xlrd.open_workbook(path)  # 打开工作簿
#     sheets = workbook.sheet_names()  # 获取工作簿中的所有表格
#     worksheet = workbook.sheet_by_name(sheets[0])  # 获取工作簿中所有表格中的的第一个表格
#     for i in range(0, worksheet.nrows):
#         for j in range(0, worksheet.ncols):
#             print(worksheet.cell_value(i, j), "\t", end="")  # 逐行逐列读取数据
#         print()


def getGoods():
    global goodsNum
    print(goodsNum)
    goodsArr = mysql.query('select * from newbook where  id > 283795 and id < 473796 and price > 0 limit %s, 1000' % goodsNum)
    # print(len(goodsArr))
    if len(goodsArr) > 0:
        goodsNum = goodsNum + 1000
        goodsData = []
        for goods in goodsArr:
            _goods = []
            _goods.append(goods['ISBN10'])
            _goods.append(goods['ISBN13'])
            _goods.append(goods['asin'])
            _goods.append(goods['reviews'])
            _goods.append(goods['ranking'])
            _goods.append(goods['packagetype'])
            _goods.append(goods['price'])
            _goods.append(goods['name'])
            _goods.append(goods['id'])
            goodsData.append(_goods)
        write_excel_xls_append(book_name_xls, goodsData)
        getGoods()
    else:
        print("======over=======")






if __name__ == '__main__':
    write_excel_xls(book_name_xls, sheet_name_xls, value_title)

    # read_excel_xls(book_name_xls)
